package com.tianchi.lexiang.util;

import java.util.ArrayList;
import java.util.List;

import android.os.Handler;
import android.util.Log;

import com.tianchi.lexiang.myinterface.UnFinishJob;

public class JobUnFinishWorker {
	private static final String TAG = "JobUnFinishWorker";
	private List<UnFinishJob> jobsNeedTodo = null;
//	private List<String> jobsDone = null;
//	private int jobCount;
//	private int jobDone;
	private Handler parent;
	private static JobUnFinishWorker instance = new JobUnFinishWorker();
	/**
	 * do the job which we need to finish.
	 * @param parent where our job will do 
	 */
	private JobUnFinishWorker(){
		jobsNeedTodo = new ArrayList<UnFinishJob>();
//		jobsDone = new ArrayList<String>();
//		jobCount = 0;
		
	}
	
	public static JobUnFinishWorker getInstance(){
		return instance;
	}

	public void setHandler(Handler handler){
		this.parent = handler;
	}
	
	public void addNewJobs(UnFinishJob job){
		Log.d(TAG, "[addNewJobs] " + job);
		jobsNeedTodo.add(job);
	}
	
	public void removeJob(UnFinishJob job){
		Log.d(TAG, "[addNewJobs] " + job);
		jobsNeedTodo.remove(job);
	}
	
	public int getJobsCount(){
		return jobsNeedTodo.size();
	}
	
	public void beginWork(){
		int jobsLength = jobsNeedTodo.size();
		Log.d(TAG, "[beginWork]...jobsLength===>" + jobsLength);
		for (int i = 0; i < jobsLength; i++) {
			Runnable job = jobsNeedTodo.get(i);
			Log.d(TAG, "[beginWork] will do job=>" + job);
			parent.post(job);
		}
		stopWork();
	}
	
	public void stopWork(){
		Log.d(TAG, "[stopWork].......");
		jobsNeedTodo.clear();
	}
}
